package com.liaowei.gateway.authority;

import cn.dev33.satoken.stp.StpInterface;
import com.liaowei.common.BaseResponse;
import com.liaowei.gateway.openfeign.AuthClient;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
@RequiredArgsConstructor
@Slf4j
public class StpInterfaceImpl implements StpInterface {
    private final AuthClient authClient;

    @SneakyThrows
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        CompletableFuture<BaseResponse<List<String>>> future = CompletableFuture.supplyAsync(() ->
                authClient.permissionNameList(Long.parseLong(loginId.toString()))
        );
        BaseResponse<List<String>> response = future.get();
        List<String> permissionNames = null;
        if (response.getCode() == 1) {
            permissionNames = response.getData();
            log.info("loginId:{} === perNames:{}", loginId.toString(),permissionNames);
        }
        return permissionNames;
    }

    @SneakyThrows
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        CompletableFuture<BaseResponse<List<String>>> future = CompletableFuture.supplyAsync(() ->
                authClient.roleNameList(Long.parseLong(loginId.toString()))
        );
        BaseResponse<List<String>> response = future.get();

        List<String> roleNames = null;
        if (response.getCode() == 1) {
            roleNames = response.getData();
            log.info("loginId:{} === roleNames:{}", loginId.toString(),roleNames);
        }
        return roleNames;
    }
}
